package com.sis.main.server;

import java.util.List;
import javax.jdo.PersistenceManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sis.main.client.DataLogin;
import com.sis.main.client.DataTrade;
import com.sis.main.client.DataSecurity;
import com.sis.main.client.intf.IntfSecuritiesPositionService;
import com.sis.main.server.ass.AmeritradeSecuritiesService;
import com.sis.main.server.jdo.GoogleDatabaseService;
import com.sis.main.server.jdo.PersistenceFactory;
import com.sis.main.server.yss.YahooSecuritiesService;

/**
 * Interaction:
 * getDataTradeQuery.execute()
 * @author Eric Atkinson, StrategicInternationalSystems.com
 */
public class SecuritiesPositionServiceImpl 
		extends RemoteServiceServlet
		implements IntfSecuritiesPositionService {	
	
		private GoogleDatabaseService googledatabaseservice;
		private AmeritradeSecuritiesService ameritradesecuritiesservice;
		private YahooSecuritiesService yahoosecuritiesservice;
		 
		
		  public DataLogin getDataLogin(DataLogin dataLogin) {	 
			  return getAmeritradeSecuritiesService().getDataLogin(dataLogin);
		  }//end login()

		
	  public DataSecurity[] getWatchList() {
		  List<DataSecurity> dataSecurities = getGoogleDatabaseService().selectWatchList();
		  DataSecurity[] dataWatchList = new DataSecurity[dataSecurities.size()];
		  if (dataSecurities.size() == 0) {
	    		return dataWatchList;
	    	}
		  int intRow = 0; 
		  for (DataSecurity dataSecurity : dataSecurities) {
			  getAmeritradeSecuritiesService().getDataLevelIQuoteUsingXMLSnapshot(dataSecurity);
			  getYahooSecuritiesService().getDataKeyStatistics(dataSecurity);
			  getGoogleDatabaseService().saveSecurity(dataSecurity); 
			  dataWatchList[intRow] = dataSecurity;
			  intRow++;	  
		  }
	    return dataWatchList;
	  }//end getWatchList()
		

	  public DataSecurity[] getQuotes(DataSecurity[] dataSecurities) {
		  if (dataSecurities == null) {
			  return dataSecurities;
		  }
		  //Date dateToday = new Date(System.currentTimeMillis());
		   PersistenceManager persistencemanager = PersistenceFactory.get().getPersistenceManager();
		    for (int i=0; i < dataSecurities.length; i++) {
		    	//query for last trade only
		    	String query = "select from " + DataTrade.class.getName()
		    		+ " where Symbol == '" + dataSecurities[i].getSymbol()
		    		+ "' range 0,0";
		    	List<DataTrade> dataTrades = (List<DataTrade>) 
		    		persistencemanager.newQuery(query).execute();
		    	if (dataTrades != null) {
		    		for (DataTrade dataTrade : dataTrades){
		    			dataSecurities[i].setLast(dataTrade.getLast());	
		    		}
		    		
		    	}
		    }
		  
	    return dataSecurities;
	  }//end getQuotes()
	
	  public DataSecurity saveSecurity(DataSecurity dataSecurity) {
		  return getGoogleDatabaseService().saveSecurity(dataSecurity);
	  }//end insertSecurity()
	  
	  public boolean deleteSecurity(DataSecurity dataSecurity) {
		  return getGoogleDatabaseService().deleteSecurity(dataSecurity);
	  }//end deleteSecurity()

		public void setAmeritradeSecuritiesService(
				AmeritradeSecuritiesService ameritradesecuritiesservice) {
			this.ameritradesecuritiesservice = ameritradesecuritiesservice;
		}

		public AmeritradeSecuritiesService getAmeritradeSecuritiesService() {
			if (this.ameritradesecuritiesservice == null) {
				setAmeritradeSecuritiesService( 
						new AmeritradeSecuritiesService() );
			}
			return ameritradesecuritiesservice;
		}

		public void setGoogleDatabaseService(
				GoogleDatabaseService googledatabaseservice) {
			this.googledatabaseservice = googledatabaseservice;
		}

		public GoogleDatabaseService getGoogleDatabaseService() {
			if (googledatabaseservice == null) {
				setGoogleDatabaseService( 
						new GoogleDatabaseService() );
			}
			return googledatabaseservice;
		}


		public void setYahooSecuritiesService(YahooSecuritiesService yahoosecuritiesservice) {
			this.yahoosecuritiesservice = yahoosecuritiesservice;
		}


		public YahooSecuritiesService getYahooSecuritiesService() {
			if (yahoosecuritiesservice == null) {
				setYahooSecuritiesService(new YahooSecuritiesService());
			}
			return yahoosecuritiesservice;
		}


}//end class SecuritiesTradingServiceImpl
